/**
 * Copyright (c) 2020-2024, huli.com
 * All rights reserved.
 */
package com.xiaohujr.xuexue.leetcode;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author Xue Xue (xuexue1@huli.com)
 * @version 1.0
 * @since 2020/9/1
 */
public class LruLinkedMap2 {

    private Map<Integer, Integer> cache;
    private int capacity;

    public LruLinkedMap2(int capacity) {
        this.cache = new LinkedHashMap<Integer, Integer>(capacity, 0.75f, true) {
            @Override
            protected boolean removeEldestEntry(Map.Entry eldest) {
                if (this.size() > capacity) {
                    return true;
                }
                return super.removeEldestEntry(eldest);
            }
        };
    }

    public Integer get(Integer key) {
        return this.cache.getOrDefault(key, -1);
    }

    public void set(Integer key, Integer value) {
        this.cache.put(key, value);
    }
}
